package com.lin.manager.secure.token;

import com.alibaba.fastjson2.JSONObject;
import com.lin.manager.secure.model.SecurityUser;
import io.jsonwebtoken.Claims;

/**
 * @author linjiang
 * @date 2024/7/25
 * @fn
 */
public class DefaultAccessTokenService implements AccessTokenService {
    private final JwtTokenGenerator generator = new JwtTokenGenerator();

    @Override
    public String createToken(SecurityUser user) {
        JSONObject object = new JSONObject();
        object.put("user", user);
        return generator.generate(JSONObject.toJSONString(object));
    }

    @Override
    public SecurityUser getUserByToken(String token) {
        Claims claims = generator.parseToken(token);
        String payload = (String) claims.get("payload");
        return JSONObject.parseObject(payload).getObject("user", SecurityUser.class);
    }

    @Override
    public void removeToken(Object userId, String token) {
    }
}
